<?php

class Rbac_model extends Model {
	/**
	 * 判断用户是否有权利访问节点
	 * @param unknown_type $uid
	 * @param unknown_type $node
	 */
	public function access($uid, $node) {
	
	}

	/**
	 * 获取用户角色
	 * @param unknown_type $uid
	 */
	public function getRole($uid) {
		$dRbac = new Rbac_data();
		$params = array('uid' => $uid);
		return $dRbac->getRole($params);
	}

	/**
	 * 获取角色权限
	 * @param unknown_type $roleid
	 */
	public function getNodeByRoleId($roleId) {
		$dRbac = new Rbac_data();
		
		$result = $dRbac->getNodeByRoleId($roleId);
		if(!$result) return false;
		$return = array();
		foreach ($result as $key => $value) {
			$return[] = $value['node_id'];
		}
		return $return;
	}
	
	/**
	 * 批量获取角色权限
	 * @param unknown_type $roleid
	 */
	public function getNodeByRoleIds($roleId) {
		
	}
	
	/**
	 * 角色分配用户
	 * @param unknown_type $uid
	 * @param unknown_type $roles
	 */
	public function role2user($roleId, $uid) {
		$dRbac = new Rbac_data();
		
		$dRbac->delRole2user($roleId);
		if($uid)return $dRbac->role2user($roleId, $uid);
		else return true;
	}
	/**
	 * 获取角色下面全部用户
	 * @param unknown_type $roleId
	 */
	public function role2userAll($roleId) {
		$dRbac = new Rbac_data();
		
		$result = $dRbac->role2userAll($roleId);
		if(!$result) return false;
		$return = array();
		foreach ($result as $key => $value) {
			$return[] = $value['uid'];
		}
		return $return;
	}
	
	/**
	 * 删除用户角色
	 * @param unknown_type $uid
	 * @param unknown_type $roleid
	 */
	public function delUser2role($uid, $roleids) {
	
	}
	
	/**
	 * 角色分配节点
	 * @param unknown_type $roleid
	 * @param unknown_type $nodeid
	 */
	public function role2node($roleId, $nodeId) {
		$dRbac = new Rbac_data();
		
		$dRbac->delRole2node($roleId);
		if($nodeId) return $dRbac->role2node($roleId, $nodeId);
		else return true;
	}
	
	/**
	 * 删除角色权限
	 * @param unknown_type $roleid
	 * @param unknown_type $nodeid
	 */
	public function delRole2node($roleid, $nodeids) {
		
	}
	
	/**
	 * 获取用户可以管理的平台
	 * @param unknown_type $uid
	 */
	public function platformByUid($uid) {
		$mUser = new User_model();
		$dRbac = new Rbac_data();
		
		$userInfo = $mUser->userInfoByUid($uid);
		$return = array();
		if($userInfo['is_supper']) {
			//获取所有平台
			$re = $dRbac->platformAll(array('pid' => 0));
		} else {
			//获取角色
			$roles = $this->getRole($uid);
			$ids = array();
			foreach ($roles as $role) {
				$ids[] = $role['role_id'];
			}
			$re = $dRbac->role2platform($ids);
		
		}
		foreach ($re as $v) {
			$return[$v['gid']] = $v['name'];
		}
		return $return;
	}
	
	/**
	 * 获取用户管理树
	 * @param unknown_type $id
	 * @param unknown_type $uid
	 */
	public function tree($gid, $uid) {
		$mUser = new User_model();
		$dRbac = new Rbac_data();
		
		$userInfo = $mUser->userInfoByUid($uid);
		$return = array();
		if($userInfo['is_supper']) {
			$re = $dRbac->treeAll($gid);
		} else {
			//获取角色
			$roles = $this->getRole($uid);
			$ids = array();
			foreach ($roles as $role) {
				$ids[] = $role['role_id'];
			}
			$re = $dRbac->role2tree($gid, $ids);
		
		}
		foreach ($re as $value) {
			$return['group'][$value['gid']] = $value['groupname'];
			$return['node'][$value['gid']][] = $value;
		}
		return $return;
	}
}